package org.groupg.poi.word;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import javax.imageio.ImageIO;

public class WatermarkImageGenerator {

    /**
     * 生成透明背景的水印图片
     * @param text 水印文字
     * @param width 图片宽度
     * @param height 图片高度
     * @param fontSize 字体大小
     * @param opacity 透明度 (0.0 - 1.0)
     * @return 图片字节数组
     */
    public static byte[] generateTextWatermark(String text, int width, int height,
                                             int fontSize, float opacity) {
        try {
            // 创建透明背景的图片
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = image.createGraphics();

            // 设置透明背景
            g2d.setComposite(AlphaComposite.Clear);
            g2d.fillRect(0, 0, width, height);
            g2d.setComposite(AlphaComposite.SrcOver);

            // 设置抗锯齿
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            // 设置字体和颜色（半透明灰色）
            Font font = new Font("微软雅黑", Font.BOLD, fontSize);
            g2d.setFont(font);
            g2d.setColor(new Color(128, 128, 128, (int)(opacity * 255))); // 半透明灰色

            // 计算文字位置（居中）
            FontMetrics metrics = g2d.getFontMetrics();
            int textWidth = metrics.stringWidth(text);
            int textHeight = metrics.getHeight();

            int x = (width - textWidth) / 2;
            int y = (height - textHeight) / 2 + metrics.getAscent();

            // 绘制文字
            g2d.drawString(text, x, y);

            // 可选：添加旋转效果
            // g2d.rotate(Math.toRadians(-45), width/2.0, height/2.0);
            // g2d.drawString(text, x, y);

            g2d.dispose();

            // 转换为字节数组
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(image, "PNG", baos);
            return baos.toByteArray();

        } catch (Exception e) {
            throw new RuntimeException("生成水印图片失败", e);
        }
    }

    /**
     * 生成带有复杂图形的透明水印
     */
    public static byte[] generateGraphicWatermark(String company, String confidential,
                                                int width, int height) {
        try {
            BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2d = image.createGraphics();

            // 透明背景
            g2d.setComposite(AlphaComposite.Clear);
            g2d.fillRect(0, 0, width, height);
            g2d.setComposite(AlphaComposite.SrcOver);

            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            // 设置旋转
            g2d.rotate(Math.toRadians(-45), width/2.0, height/2.0);

            // 绘制主标题
            Font mainFont = new Font("宋体", Font.BOLD, 48);
            g2d.setFont(mainFont);
            g2d.setColor(new Color(200, 200, 200, 80)); // 很浅的灰色，低透明度

            FontMetrics mainMetrics = g2d.getFontMetrics();
            int mainWidth = mainMetrics.stringWidth(company);
            int mainX = (width - mainWidth) / 2;
            int mainY = height / 2;
            g2d.drawString(company, mainX, mainY);

            // 绘制副标题
            Font subFont = new Font("宋体", Font.PLAIN, 24);
            g2d.setFont(subFont);
            g2d.setColor(new Color(150, 150, 150, 60));

            FontMetrics subMetrics = g2d.getFontMetrics();
            int subWidth = subMetrics.stringWidth(confidential);
            int subX = (width - subWidth) / 2;
            int subY = mainY + 40;
            g2d.drawString(confidential, subX, subY);

            g2d.dispose();

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(image, "PNG", baos);
            return baos.toByteArray();

        } catch (Exception e) {
            throw new RuntimeException("生成图形水印失败", e);
        }
    }
}